package com.example.fengyungo.service.impl;

import com.example.fengyungo.constant.MailConstant;
import com.example.fengyungo.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送验证码到司机用户邮箱
     *
     * @param to 目标用户邮箱
     */
    @Override
    public Boolean sendCodeMailToDriver(String to) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送人
        String from = "xlr20031006@163.com";
        message.setFrom(from + "(风运Go)");
        message.setTo(to);
        // 标题
        String subject = "风运GO-司机端验证";
        message.setSubject(subject);
        // 生成随机6位验证码
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            code += (random.nextInt(10));
        }
        String text = "【风运Go-司机端】用户，您的验证码是: " + code + "，请在5分钟内使用。本验证码将用于风运Go账号操作，为了您的账号安全，请勿告诉任何人！";
        message.setText(text);
        //获取redis操作类
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 发送邮件
        javaMailSender.send(message);
        String key = MailConstant.DRIVER_IDENTITY +to;
        valueOperations.set(key, code);
        // 设置5分钟过期
        redisTemplate.expire(key,60*5, TimeUnit.MINUTES);
        return true;
    }

    /**
     * 对验证码进行验证
     *
     * @param identity 身份标识
     * @param email 邮箱
     * @param code 验证码
     * @return 验证结果
     */
    @Override
    public Boolean verificationCode(String identity,String email, String code) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 从redis中获取验证码
        String key = identity+email;
        String verificationCode = (String) valueOperations.get(key);
        System.out.println(verificationCode);
        // 判断提交的信息是否正确
        if (verificationCode != null && verificationCode.equals(code)) {
            // 删除缓存中的数据
            redisTemplate.delete(key);
            return true;
        }
        return false;
    }

    /**
     * 发送验证码到普通用户邮箱
     *
     * @param to 目标用户邮箱
     */
    @Override
    public Boolean sendCodeMailToUser(String to) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送人
        String from = "xlr20031006@163.com";
        message.setFrom(from + "(风运Go)");
        message.setTo(to);
        // 标题
        String subject = "风运GO-用户端验证";
        message.setSubject(subject);
        // 生成随机6位验证码
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            code += (random.nextInt(10));
        }
        String text = "【风运Go-用户端】用户，您的验证码是: " + code + "，请在5分钟内使用。本验证码将用于风运Go账号操作，为了您的账号安全，请勿告诉任何人！";
        message.setText(text);
        //获取redis操作类
        ValueOperations valueOperations = redisTemplate.opsForValue();
        // 发送邮件
        javaMailSender.send(message);
        String key = MailConstant.USER_IDENTITY +to;
        valueOperations.set(key, code);
        // 设置5分钟过期
        redisTemplate.expire(key,60*5, TimeUnit.MINUTES);
        return true;
    }

}
